package com.fuxian.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fuxian.model.Student;
import com.fuxian.service.StudentService;
import com.fuxian.util.DateUtile;
import com.fuxian.util.ReturnMapUtil;

@Controller
@Scope("prototype")
public class StudentAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(StudentAction.class);

	@Resource
	private StudentService studentService;

	/* ========================================= */
	private int student_id;
	private String name;
	private String school;
	private String grade;
	private String clazz;
	private String edu_bg;
	private String remark1;
	private String intro;
	private String photo_path;

	private int rows;
	private int page;

	public void setStudent_id(int studentId) {
		student_id = studentId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public void setEdu_bg(String eduBg) {
		edu_bg = eduBg;
	}

	public void setRemark1(String remark1) {
		this.remark1 = remark1;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public void setPhoto_path(String photoPath) {
		photo_path = photoPath;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

	/* ========================================= */

	public String addStudent() {
		returnMap = new HashMap<String, Object>();
		Student student = new Student();
		student.setName(name);
		student.setClazz(clazz);
		student.setEdu_bg(edu_bg);
		student.setGrade(grade);
		student.setSchool(school);
		student.setIntro(intro);
		if (photo_path == null) {
			photo_path = "";// 默认照片
		}
		student.setPhoto_path(photo_path);
		student.setRemark1(remark1);
		student.setCreate_time(DateUtile.getTimestamp());
		student.setCreate_user((String) session.get("user_name"));
		studentService.addStudent(student);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String updateStudent() {
		returnMap = new HashMap<String, Object>();
		Student student = new Student();
		student.setStudent_id(student_id);
		student.setName(name);
		student.setClazz(clazz);
		student.setEdu_bg(edu_bg);
		student.setGrade(grade);
		student.setSchool(school);
		student.setIntro(intro);
		student.setPhoto_path(photo_path);
		student.setRemark1(remark1);
		studentService.modifyStudent(student);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String deleteStudent() {
		returnMap = new HashMap<String, Object>();
		studentService.deleteStudentById(student_id);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String getStudentList() {
		returnMap = new HashMap<String, Object>();
		page = ((page == 0) ? 1 : page);
		rows = ((rows == 0) ? 1 : rows);
		int totalCount = studentService.selectCount();
		int max_page = (totalCount + rows - 1) / rows;
		returnMap.put("max_page", max_page);
		returnMap.put("page", page);
		returnMap.put("total", totalCount);
		List<Student> studentList = studentService.selectAllStudent(rows
				* (page - 1), rows);
		returnMap.put("rows", studentList);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String getStudentInfo() {
		returnMap = new HashMap<String, Object>();
		Student student = studentService.selectStudentById(student_id);
		returnMap.put("name", student.getName());
		returnMap.put("school", student.getSchool());
		returnMap.put("grade", student.getGrade());
		returnMap.put("intro", student.getIntro());
		returnMap.put("edu_bg", student.getEdu_bg());
		returnMap.put("clazz", student.getClazz());
		returnMap.put("student_id", student.getStudent_id());
		returnMap.put("photo_path", student.getPhoto_path());
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

}
